home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / tasippgpkey.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.0 KB  |  96 lines

  1. //    TASIPPGPkey.cp - AppleShare PGP Key Object  
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinnie Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include <string.h>
  18. #include "TASIPPGPkey.h"
  19. #include "TPGPException.h"
  20.  
  21.  
  22.  
  23. // ---------------------------------------------------------------------------
  24. void TASIPPGPkey::Initialize(const unsigned char* serverSigBuf)
  25. // ---------------------------------------------------------------------------
  26. {     
  27.     PGPFilterRef    theFilter         = kInvalidPGPFilterRef;
  28.     PGPKeyIterRef    theIterator     = kInvalidPGPKeyIterRef;
  29.     PGPKeyListRef    theKeyListRef     = kInvalidPGPKeyListRef;
  30.     PGPKeyRef        bestKey             = kInvalidPGPKeyRef;
  31.     PGPKeyRef        expiredKey         = kInvalidPGPKeyRef;
  32.     PGPKeyRef        aKey             = kInvalidPGPKeyRef;
  33.     PGPKeySetRef    newKeySet         = NULL;
  34.     PGPUInt32         numKeys;
  35.     
  36.     const     char hexDigit[] = "0123456789ABCDEF";
  37.      char    *p;
  38.     int        strIndex;
  39.  
  40. // calculate server name
  41.       strcpy(fNameString, "asip:");
  42.      p = &fNameString[5];
  43.       for(strIndex = 0 ; strIndex < 16 ; strIndex++)
  44.     {
  45.         *p++ = hexDigit[serverSigBuf[strIndex]>>4];
  46.         *p++ = hexDigit[serverSigBuf[strIndex]&0xF];
  47.     };
  48.     *p = '\0';
  49.       
  50. // Find key in database
  51.       ThrowIfPGPErr( PGPNewUserIDEmailFilter(fgContext,fNameString, kPGPMatchEqual, &theFilter));
  52.     ThrowIfPGPErr( PGPFilterKeySet(fgPGPKeySetRef , theFilter, &newKeySet));
  53.     PGPFreeFilter(theFilter);
  54.     ThrowIfPGPErr( PGPOrderKeySet(newKeySet,kPGPAnyOrdering, &theKeyListRef));
  55.     ThrowIfPGPErr( PGPNewKeyIter( theKeyListRef, &theIterator ));
  56.     
  57.      ThrowIfPGPErr( PGPCountKeys(newKeySet, &numKeys));
  58.  
  59.     if(numKeys > 0) 
  60.     {
  61.         Boolean canVerify, isExpired;
  62.  
  63.         while(IsntPGPError (PGPKeyIterNext( theIterator, &aKey )))
  64.          {
  65.              ThrowIfPGPErr( PGPGetKeyBoolean (aKey, kPGPKeyPropCanVerify,  &canVerify));
  66.              ThrowIfPGPErr( PGPGetKeyBoolean (aKey, kPGPKeyPropIsExpired,  &isExpired));
  67.             
  68.             if(canVerify)
  69.                 if(!isExpired) bestKey = aKey;
  70.                 else expiredKey = aKey;
  71.           } 
  72.   
  73.       if ( PGPKeyRefIsValid( bestKey ) ) TPGPkey::Initialize( bestKey );
  74.       else if (PGPKeyRefIsValid(expiredKey) ) TPGPkey::Initialize( expiredKey );
  75.       }
  76.       
  77.     PGPFreeKeyIter(theIterator );
  78.     PGPFreeKeyList(theKeyListRef );
  79.     PGPFreeKeySet(newKeySet );
  80.  
  81.  }
  82.  
  83.  
  84. // ---------------------------------------------------------------------------
  85. void TASIPPGPkey::GetKeyNamePString(StringPtr buf)
  86. // ---------------------------------------------------------------------------
  87. {
  88.     if(buf)
  89.     {
  90.         memcpy(fNameString, &buf[1], strlen(fNameString));
  91.         buf[0] = strlen(fNameString);
  92.     }
  93.  
  94. }
  95.  
  96.